package com.redlight.base;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by chao on 2019/1/9.
 */
@Repository
public class BaseDao <T> extends SqlSessionDaoSupport {
    protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
//    private static SqlSession sqlSession = (SqlSession) ApplicationContextProvider.getBean("SqlSessionFactory");
    @Resource
    private SqlSessionFactory sqlSessionFactory;
    @Resource
    private SqlSessionTemplate sqlSessionTemplate;
    @Resource
    private SqlSession sqlSession;
    @Resource
    private JdbcTemplate jdbcTemplate;
    public BaseDao() {
    }
    public void setSessionTemplate(SqlSessionTemplate sessionTemplate) {
        this.sqlSessionTemplate = sessionTemplate;
    }
    @Autowired
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }

    public JdbcTemplate getJdbcTemplate() {
        return this.jdbcTemplate;
    }

    public Object create(String sqlId, Object paramObj) {
        return Integer.valueOf(this.sqlSession.insert(sqlId, paramObj));
    }

    public boolean update(String sqlId, Object paramObj) {
        int count = this.sqlSession.update(sqlId, paramObj);
        return count > 0;
    }

    public boolean delete(String sqlId, Object paramObj) {
        int count = this.sqlSession.delete(sqlId, paramObj);
        return count > 0;
    }

    public boolean isExist(String sqlId, Object paramObj) {
        List dataList = this.sqlSession.selectList(sqlId, paramObj);
        return null != dataList && 0 < dataList.size();
    }

    public <T> T get(String sqlId, Object paramObj) {
        return this.sqlSession.selectOne(sqlId, paramObj);
    }

    public List<T> findList(String sqlId, Object paramObj) {
        List dataList = this.sqlSession.selectList(sqlId, paramObj);
        return (List)(dataList != null?dataList:new ArrayList());
    }

    public Page findPage(String sqlId, Object paramObj) {
        Page retPage = new Page();
        Integer total = (Integer)this.sqlSession.selectOne(sqlId + "_count", paramObj);
        if(null != total && 0 < total.intValue()) {
            if(paramObj instanceof Page) {
                retPage = (Page)paramObj;
                retPage.setCount(total);
            } else if(paramObj instanceof Map) {
                Map paraMap = (Map)paramObj;
                retPage = WebUtil.getParameterPage(paraMap);
                retPage.setCount(total);
                paraMap.put("page", retPage);
            } else if(paramObj instanceof BaseEntity) {
                retPage = ((BaseEntity)paramObj).getPage();
                retPage.setCount(total);
            } else if(null != paramObj) {
                Object pageField = Reflections.getFieldValue(paramObj, "page");
                if(null != pageField) {
                    retPage = (Page)pageField;
                    retPage.setCount(total);
                }
            }

            List dataList = this.sqlSession.selectList(sqlId, paramObj);
            retPage.setRows(dataList);
        }

        return retPage;
    }

    public void execProc(String procId, Object obj) {
        this.sqlSession.insert(procId, obj);
    }
}
